//
//  AListController.m
//  MClient
//
//  Created by  wangfei on 10-3-31.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "AListController.h"
#import "MClientAppDelegate.h"
#import "MServerDAO.h"
#import "MCFunction.h"
#import "QueryController.h"
#import "MCUIToolkit.h"
#import "EGORefreshTableHeaderView.h"
#import "WithFormButtonsBar.h"

@interface AListController (Private)
//{

//CGPoint startPoint;

//int actionSheetFlag;
//}
- (void)refreshFinished;
@end

@implementation AListController

@synthesize dataObjectName;
@synthesize layoutName;
@synthesize queryFilter;
@synthesize page;
@synthesize listData;
@synthesize listLayout;
@synthesize dataRefresh;

// 初始化view
- (void)viewDidLoad {
	[super viewDidLoad];

	self.title = [listLayout objectForKey:@"lbl"];
	self.tableview.backgroundColor = [UIColor clearColor];
	self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
	
	cellWidth = self.width;
	cellHeight = 0.0;
	imageWidth = 0.0;
	imageHeight = 0.0;
	imagesCache = [[NSMutableDictionary alloc] init];
	
	[self leftBarButton:@"返回" withStyle:UIBarButtonItemStylePlain selector:@selector(selectCancelAction:)];
	
	isJump2Page = ![[[listLayout objectForKey:@"csty"] objectForKey:@"scroll2page"] intValue];
	if (isJump2Page) {
		pageBar = [MCUIToolkit getPageBar:[MCUIToolkit hasQueryField:listLayout] delegate:self];
		[self.clientView addSubview:pageBar];
		pageBar.frame = CGRectMake(0, self.height-nToolBarHeight, self.width, nToolBarHeight);
		pageBar.autoresizingMask = nMCViewAutoresizingMask;
		self.tableview.frame = CGRectMake(0, 0, self.width, self.height-pageBar.frame.size.height);
		pageBar.hidden = YES;
	}
	buttonCol = 1;
	if ([listLayout objectForKey:@"csty"]) {
		if ([[listLayout objectForKey:@"csty"] objectForKey:@"button_col"]) {
			buttonCol =  [[[listLayout objectForKey:@"csty"] objectForKey:@"button_col"] intValue];
			if (buttonCol > 3) {
				buttonCol = 3;
			}
		}
	}
	// 根据配置显示/隐藏帮助按钮
	self.addHelpButton = YES;
	self.helpUrl = @"/iphone/help/chapter04.html";
	aListInfo = [[NSMutableDictionary alloc] init];
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	
	if (isJump2Page) {
		int nHeight = CGRectGetHeight(pageBar.frame);
		tableview.frame = CGRectMake(0, 0, self.width, self.height-nHeight);
		pageBar.frame = CGRectMake(0, self.height-nHeight, self.width, nHeight);
	}
}

- (void)dealloc {
	[pageBar release];
	[dataObjectName release];
	[layoutName release];
	[queryFilter release];
	[listData release];
	[listLayout release];
	[listButtons release];
	[listDescButtons release];
	[aListInfo release];
	[withFormButtons release];
	[imagesCache release];
	[refreshHeaderView release];
	[refreshHeaderView_Up release];
    [super dealloc];
}

#pragma mark ---UIToolbar事件---
-(void)goFirst:(id)inSender {
	dataRefresh = NO;
	if (page != 1) {
		page = 1;
		[self getDataFromServer];
	}
}

-(BOOL)goPrevious:(id)inSender {
	dataRefresh = NO;
	if (page > 1) {
		page = page - 1;
		[self getDataFromServer];
		return YES;
	} else {
		return NO;
	}
}

-(BOOL)goNext:(id)inSender {
	dataRefresh = NO;
	if (page < totalPage) {
		page = page + 1;
		[self getDataFromServer];
		return YES;
	} else {
		return NO;
	}
}

-(void)goLast:(id)inSender {
	dataRefresh = NO;
	if (page != totalPage) {
		page = totalPage;
		[self getDataFromServer];
	}
}

-(void)gotoPage:(id)inSender {
	dataRefresh = NO;
	UITextField *inputView = (UITextField *)[((UIBarButtonItem *)[pageBar.items objectAtIndex:4]).customView viewWithTag:1];
	int goPage = [inputView.text intValue];
	if (goPage >= 1 && goPage <= totalPage) {
		page = goPage;
		[self getDataFromServer];
	}
}

-(void)reload:(id)inSender {
	dataRefresh = NO;
	[self getDataFromServer];
}

-(void)search:(id)inSender {
	QueryController *queryController = [[QueryController alloc] initWithStyle:UITableViewStylePlain];
	queryController.listLayout = self.listLayout;
	queryController.queryFilter = self.queryFilter;
	queryController.queryFirst = NO;
	[self.navigationController pushViewController:queryController animated:YES];
	[queryController release];
}

#pragma mark ---- 数据加载方法 ----
- (void)getDataFromServer {
	if (dataRefresh && !isJump2Page) {
		page = 1;
	}
	[(UITextField *)[((UIBarButtonItem *)[pageBar.items objectAtIndex:4]).customView viewWithTag:1] resignFirstResponder];
	[imagesCache removeAllObjects];
	[[MServerDAO sharedInstance] getListDataAsync:dataObjectName 
									   layoutName:layoutName 
									  queryFilter:queryFilter 
											 page:page
										 delegate:self ];
}

- (void)getDataFromServerSuccess: (NSMutableDictionary *)returnValue {
	self.tableview.backgroundColor = [MCUserDefaults instance].listViewCellRectColor_Even;
	
	if (isJump2Page || [MCUserDefaults instance].dragListLastAndMore) {
		self.listData = [returnValue objectForKey:@"rl"];
	} else {
		if (self.listData == nil) {
			self.listData = [NSMutableArray arrayWithArray:[returnValue objectForKey:@"rl"]];
		} else {
			[self.listData addObjectsFromArray:[returnValue objectForKey:@"rl"]];
		}
	}

	if (dataRefresh && !isJump2Page) {
		[self.listData removeAllObjects];
		self.listData = [returnValue objectForKey:@"rl"];
	}
	
	[self refreshFinished];
	
	totalPage = 1;
	if ([returnValue objectForKey:@"tp"]) {
		totalPage = [[returnValue objectForKey:@"tp"] intValue];
	}
	if (totalPage == 0) {
		totalPage = 1;
	}
	
	//if (totalPage < 2 && pageBar) {
//		[pageBar removeFromSuperview];
//		[pageBar release];
//		pageBar = nil;
//		self.tableview.frame = CGRectMake(0, 0, self.width, self.height);
//	} else {
		pageBar.hidden = NO;
//	}
	
	if (pageBar && [pageBar.items count] > 5) {
		UITextField *inputView = (UITextField *)[((UIBarButtonItem *)[pageBar.items objectAtIndex:4]).customView viewWithTag:1];
		inputView.text = @"";
		inputView.placeholder = [NSString stringWithFormat:@"%d", page];
		UILabel *pageLabel = (UILabel *)(UITextField *)[((UIBarButtonItem *)[pageBar.items objectAtIndex:4]).customView viewWithTag:2];
		pageLabel.text = [NSString stringWithFormat:@"/%d", totalPage];
	}
	
	NSMutableArray *dataButtons = [returnValue objectForKey:@"btn"];
	NSMutableArray *cfgButtons = [listLayout objectForKey:@"btn"];
	if (listButtons) {
		[listButtons release];listButtons = nil;
	}
	listButtons = [[MCUIToolkit getButtons:dataButtons cfgButtons:cfgButtons] retain];	
	
	withFormButtons = [MCUIToolkit getWithFormButtons:listButtons];
	int withFormButtonsBarHeight = [MCUIToolkit getWithFormButtonsBarHeight:withFormButtons buttonCol:buttonCol];
	if (withFormButtonsBarHeight != 0) {
		WithFormButtonsBar *withFormButtonsBar = [[WithFormButtonsBar alloc] initWithFrame:CGRectMake(0, 0, self.width, withFormButtonsBarHeight)];
		withFormButtonsBar.tag = -8888;
		[withFormButtonsBar addWithFormButtons:withFormButtons buttonCol:buttonCol delegate:self];
		self.tableview.tableFooterView = withFormButtonsBar;
		[withFormButtonsBar release];
	}
	[self rightBarButton:@"操作" withStyle:UIBarButtonItemStyleDone selector:@selector(selectAction:)];
	if ([listButtons count] == 1) {
		self.rightBarButtonItemTitle = [[listButtons objectAtIndex:0] objectForKey:@"lbl"];
	} else if ([listButtons count] > 1) {
		self.rightBarButtonItemTitle = @"操作";
	} else {
		self.rightBarButtonItem = nil;
	}
	
	//动态表单标签
	if ([returnValue objectForKey:@"csty"]) {
		NSMutableDictionary *csty = [returnValue objectForKey:@"csty"];
		if ([csty objectForKey:@"label"]) {
			self.title = [csty objectForKey:@"label"];
		}
	}
	
	NSMutableArray *listFields = [listLayout objectForKey:@"item"];
	if ([listData count] > 0) {
		NSMutableDictionary *record = [listData objectAtIndex:0];
		for (int i = 0; i < [listFields count]; i++) {
			NSMutableDictionary *appItem = [listFields objectAtIndex:i];
			if ([[appItem objectForKey:@"v"] boolValue]) {
				NSString *fieldType = [MCUIToolkit getFieldType:appItem];
				NSString *fieldName = [MCUIToolkit getFieldName:appItem];
				NSMutableDictionary *fieldData = [[record objectForKey:@"data"] objectForKey:fieldName];
				if ([fieldType isEqualToString:@"S"]) {
					imageWidth = nImageWidthOfCell;
					imageHeight = nImageHeightOfCell;
					NSMutableDictionary *style = [fieldData objectForKey:@"style"];
					if (nil != style) {
						if ([style objectForKey:@"width"]) {
							imageWidth = [[style objectForKey:@"width"] floatValue];
						}
						if ([style objectForKey:@"height"]) {
							imageHeight = [[style objectForKey:@"height"] floatValue];
						}
					}
					if (imageWidth > self.width) {
						imageHeight = self.width * imageHeight / imageWidth;
						imageWidth = self.width;
					}
					cellWidth = self.width - imageWidth;
				}
			}
		}
	}
	if ([returnValue objectForKey:@"csty"]) {
		NSMutableDictionary *csty = [returnValue objectForKey:@"csty"];
		if ([csty objectForKey:@"label"]) {
			self.title = [csty objectForKey:@"label"];
		}
	}
	if ([self.view viewWithTag:-963]) {
		[[self.view viewWithTag:-963] removeFromSuperview];
	}
	if ([self.listData count] == 0) {
		self.tableview.hidden = YES;
		UILabel* label = [[UILabel alloc] initWithFrame:self.view.bounds];
		label.text = @"当前无数据！";
		label.backgroundColor = [UIColor clearColor];
		label.textAlignment = UITextAlignmentCenter;
		label.tag = -963;
		[self.view addSubview:label];
		[label release];
	}
	[self reloadData];
}


#pragma mark ---- UITableView处理方法 ----

//TableView的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [self.listData count];	
}

//每一行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	int row = [indexPath row];
	
	NSMutableArray *cellInfos = [MCUIToolkit getAListInfo:listLayout
												 listData:self.listData
												 rowIndex:row
												cellWidth:cellWidth];
	[aListInfo setObject:cellInfos forKey:[NSNumber numberWithInt:row]];
	if ([cellInfos count] <= 0) {
		return 0.0;
	}
	cellHeight = [[[cellInfos objectAtIndex:([cellInfos count] - 1)] objectForKey:@"cellHeight"] floatValue];
	if (cellHeight < imageHeight) {
		cellHeight = imageHeight + 12.0;
		return cellHeight;
	}
	if (cellHeight == 0.0) {
		return cellHeight;
	}
	if (cellHeight < 28.0) {
		return 44.0;
	}
	return cellHeight + 12.0;
}

//初始化TableVie根据参数和数据绘制每一行以及填充数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	int row = [indexPath row];
	UITableViewCell *cell = [MCUIToolkit getAListCell:[aListInfo objectForKey:[NSNumber numberWithInt:row]]
										  imagesCache:imagesCache
											 rowIndex:row
										isMultiSelect:NO
											cellWidth:cellWidth
										   cellHeight:cellHeight
										   imageWidth:imageWidth
										  imageHeight:imageHeight];
	
	NSMutableDictionary *record = [self.listData objectAtIndex:indexPath.row];
	NSMutableArray *dataButtons = [record objectForKey:@"btn"];
	NSMutableArray *cfgButtons =  [listLayout objectForKey:@"dbtn"];
	NSMutableArray *descButtons = [MCUIToolkit getButtons:dataButtons cfgButtons:cfgButtons];
	if (descButtons && [descButtons count] > 0) {
		cell.selectionStyle = UITableViewCellSelectionStyleBlue;
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	} else {
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		cell.accessoryType = UITableViewCellAccessoryNone;
	}

	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {	
	int row = [indexPath row];
	actionSheetFlag = row;
	
	NSMutableDictionary *record = [self.listData objectAtIndex:row];	
	
	NSMutableArray *dataButtons = [record objectForKey:@"btn"];
	NSMutableArray *cfgButtons =  [listLayout objectForKey:@"dbtn"];
	if (listDescButtons) {
		[listDescButtons release];listDescButtons = nil;
	}
	listDescButtons = [[MCUIToolkit getButtons:dataButtons cfgButtons:cfgButtons] retain];
	
	if ([listDescButtons count] <= 0) {
		return;
	}
	if ([listDescButtons count] == 1) {
		NSString *pKey = @"";
		if ([record objectForKey:@"key"] != nil) {
			pKey = [record objectForKey:@"key"];
		}
		NSMutableDictionary *button = [listDescButtons objectAtIndex:0];
		if ([MCErrorManagement showAuthMessage:button]) {
			return;
		}
		NSString *buttonId = [button objectForKey:@"id"] == nil?@"":[button objectForKey:@"id"];
		NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
										   buttonId,@"buttonId",
										   @"list_rec",@"buttonType",
										   self.dataObjectName,@"dataObjectName",
										   record,@"formData",
										   pKey, @"primaryKey", nil];
		
		if ([button objectForKey:@"cfmi"] != nil && ![@"" isEqualToString:[button objectForKey:@"cfmi"]]){
			[MCFunction pushViewByUrl:[button objectForKey:@"url"] parameters:parameters confirmMessage:[button objectForKey:@"cfmi"] naviController:self.navigationController];
		}else{
			[MCFunction pushViewByUrl:[button objectForKey:@"url"] parameters:parameters confirmMessage:nil naviController:self.navigationController];
		}
		[parameters release];
	} else if ([listDescButtons count] > 1) {
		UIActionSheet *menu = [MCUIToolkit actionSheet:listDescButtons
											   withTag:0
										  withDelegate:self
											 withTitle:@"请选择明细操作"];
		[menu showInView:self.clientView.window];
	}
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)withFormButtonsAction:(id) sender {
	NSMutableDictionary *button = [withFormButtons objectAtIndex:((UIButton *)sender).tag];
	if ([MCErrorManagement showAuthMessage:button]) {
		return;
	}
	NSString *buttonId = [button objectForKey:@"id"] == nil?@"":[button objectForKey:@"id"];
	NSMutableDictionary *url = [button objectForKey:@"url"];
	NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
									   buttonId,@"buttonId",
									   @"list_form",@"buttonType",
									   self.dataObjectName,@"dataObjectName",nil];
	if ([button objectForKey:@"cfmi"] != nil && ![@"" isEqualToString:[button objectForKey:@"cfmi"]]){
		[MCFunction pushViewByUrl:url	
					   parameters:parameters
				   confirmMessage:[button objectForKey:@"cfmi"] 
				   naviController:self.navigationController];
	}else {
		[MCFunction pushViewByUrl:url	
					   parameters:parameters
				   confirmMessage:nil 
				   naviController:self.navigationController];
	}
	[parameters release];	
}

//导航栏上动作按钮的点击事件
- (void) selectAction: (id) sender {
	if ([listButtons count] == 1) {
		NSMutableDictionary *button = [listButtons objectAtIndex:0];
		if ([MCErrorManagement showAuthMessage:button]) {
			return;
		}
		NSString *buttonId = [button objectForKey:@"id"] == nil?@"":[button objectForKey:@"id"];
		NSMutableDictionary *url = [button objectForKey:@"url"];
		NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
										   buttonId,@"buttonId",
										   @"list_form",@"buttonType",
										   self.dataObjectName,@"dataObjectName",nil];
		if ([button objectForKey:@"cfmi"] != nil && ![@"" isEqualToString:[button objectForKey:@"cfmi"]]){
			[MCFunction pushViewByUrl:url	
						   parameters:parameters
					   confirmMessage:[button objectForKey:@"cfmi"] naviController:self.navigationController];
		}else {
			[MCFunction pushViewByUrl:url	
						   parameters:parameters
					   confirmMessage:nil naviController:self.navigationController];
		}
		[parameters release];
		return;
	}	
	
	actionSheetFlag = -1;
	UIActionSheet *menu = [MCUIToolkit actionSheet:listButtons
										   withTag:0
									  withDelegate:self
										 withTitle:@"选择操作"];
	[menu showFromTabBar:(UITabBar *)[self.navigationController view]];
}


//弹出菜单按钮点击事件，当actionSheetFlag等于-1的时候执行列表动作，否则执行列表明细动作
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
	if (actionSheetFlag == -1) {
		if (buttonIndex == [listButtons count]) {
			// 按下取消按钮
			return;
		}
		NSMutableDictionary *button = [listButtons objectAtIndex:buttonIndex];
		if ([MCErrorManagement showAuthMessage:button]) {
			return;
		}
		NSString *buttonId = [button objectForKey:@"id"] == nil?@"":[button objectForKey:@"id"];
		NSMutableDictionary *url = [button objectForKey:@"url"];
		NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
										   buttonId,@"buttonId",
										   @"list_form",@"buttonType",
										   self.dataObjectName,@"dataObjectName",nil];
		if ([button objectForKey:@"cfmi"] != nil && ![@"" isEqualToString:[button objectForKey:@"cfmi"]]){
			[MCFunction pushViewByUrl:url	
						   parameters:parameters
					   confirmMessage:[button objectForKey:@"cfmi"] naviController:self.navigationController];
		}else {
			[MCFunction pushViewByUrl:url	
						   parameters:parameters
					   confirmMessage:nil naviController:self.navigationController];
		}
		[parameters release];
	}
	else {
		if (buttonIndex == [listDescButtons count]) {
			// 按下取消按钮
			return;
		}
		NSMutableDictionary *record = [self.listData objectAtIndex:actionSheetFlag];
		
		NSMutableDictionary *button = [listDescButtons objectAtIndex:buttonIndex];
		if ([MCErrorManagement showAuthMessage:button]) {
			return;
		}
		NSString *pKey = @"";
		if ([record objectForKey:@"key"] != nil) {
			pKey = [record objectForKey:@"key"];
		}
		NSString *buttonId = [button objectForKey:@"id"] == nil?@"":[button objectForKey:@"id"];
		NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
										   buttonId,@"buttonId",
										   @"list_rec",@"buttonType",
										   self.dataObjectName,@"dataObjectName",
										   record,@"formData",
										   pKey, @"primaryKey", nil];
		
		if ([button objectForKey:@"cfmi"] != nil && ![@"" isEqualToString:[button objectForKey:@"cfmi"]]){
			[MCFunction pushViewByUrl:[button objectForKey:@"url"] parameters:parameters confirmMessage:[button objectForKey:@"cfmi"] naviController:self.navigationController];
		}else{
			[MCFunction pushViewByUrl:[button objectForKey:@"url"] parameters:parameters confirmMessage:nil naviController:self.navigationController];
		}
		[parameters release];
	}
}

- (void) selectCancelAction: (id) sender {
	/*UIViewController *viewController = [self.navigationController.viewControllers objectAtIndex:[self.navigationController.viewControllers count] - 2];
	if ([viewController isMemberOfClass:[QueryController class]]){
		[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:[self.navigationController.viewControllers count] - 3] animated:YES];
		return;
	}*///huotianjun 注释掉，querycontroller的加载后，不能返回。
	[self.navigationController popViewControllerAnimated:YES];
}

- (void)reloadData
{
	[self.tableview reloadData];
	if (refreshHeaderView) {
		[refreshHeaderView removeFromSuperview];
		[refreshHeaderView release];
		refreshHeaderView = nil;
	}
	if (refreshHeaderView_Up) {
		[refreshHeaderView_Up removeFromSuperview];
		[refreshHeaderView_Up release];
		refreshHeaderView_Up = nil;
	}
	
	if (!isJump2Page && totalPage > 1) {
		CGRect frame = CGRectMake(0.0f, self.tableview.contentSize.height, self.tableview.bounds.size.width, 65);
		refreshHeaderView = [[EGORefreshTableHeaderView alloc] initWithFrame:frame withPage:page withTotlePage:totalPage withFlag:YES];
		refreshHeaderView.backgroundColor = [UIColor clearColor];
		[self.tableview addSubview:refreshHeaderView];
		
		if ([MCUserDefaults instance].dragListLastAndMore) {
			frame = CGRectMake(0.0f, -65, self.tableview.bounds.size.width, 65.0);
			refreshHeaderView_Up = [[EGORefreshTableHeaderView alloc] initWithFrame:frame withPage:page withTotlePage:totalPage  withFlag:NO];
			refreshHeaderView_Up.backgroundColor = [UIColor clearColor];
			[self.tableview addSubview:refreshHeaderView_Up];
		}
	}
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
	int nScrollHeight = scrollView.bounds.size.height;
	
	if (!_reloading && refreshHeaderView && 
		scrollView.contentOffset.y+nScrollHeight >= scrollView.contentSize.height+65 &&
		[self goNext:nil]) {
		_reloading = YES;
		[refreshHeaderView setState:EGOOPullRefreshLoading];
		[UIView beginAnimations:nil context:NULL];
		[UIView setAnimationDuration:0.2];
		scrollView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, 60.0f, 0.0f);
		[UIView commitAnimations];
	}
	
	if ([MCUserDefaults instance].dragListLastAndMore) {
		if (scrollView.contentOffset.y <= - 65.0f && !_reloading_up && [self goPrevious:nil]) {
			_reloading_up = YES;
			[refreshHeaderView_Up setState:EGOOPullRefreshLoading];
			[UIView beginAnimations:nil context:NULL];
			[UIView setAnimationDuration:0.2];
			scrollView.contentInset = UIEdgeInsetsMake(60.0f, 0.0f, 0.0f, 0.0f);
			[UIView commitAnimations];
		}		
	}
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{	
	
	if (scrollView.isDragging) {
		int nScrollHeight = scrollView.bounds.size.height;
		if (refreshHeaderView.state == EGOOPullRefreshPulling && refreshHeaderView &&
			scrollView.contentOffset.y+nScrollHeight >= scrollView.contentSize.height && 
			scrollView.contentOffset.y+nScrollHeight < scrollView.contentSize.height+65 && !_reloading) {
			[refreshHeaderView setState:EGOOPullRefreshNormal];
		} else if (refreshHeaderView.state == EGOOPullRefreshNormal && refreshHeaderView &&
				   scrollView.contentOffset.y+nScrollHeight >= scrollView.contentSize.height+65 && !_reloading) {
			[refreshHeaderView setState:EGOOPullRefreshPulling];
		}
		
		if ([MCUserDefaults instance].dragListLastAndMore) {
			if (refreshHeaderView_Up.state == EGOOPullRefreshPulling && scrollView.contentOffset.y > -65.0f && scrollView.contentOffset.y < 0.0f && !_reloading_up) {
				[refreshHeaderView_Up setState:EGOOPullRefreshNormal];
			} else if (refreshHeaderView_Up.state == EGOOPullRefreshNormal && scrollView.contentOffset.y < -65.0f && !_reloading_up) {
				[refreshHeaderView_Up setState:EGOOPullRefreshPulling];
			}
		}
	}
}

- (void)refreshFinished{
	
	_reloading = NO;
	
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:.3];
	[self.tableview setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];
	[UIView commitAnimations];
	
	[refreshHeaderView setState:EGOOPullRefreshNormal];
	[refreshHeaderView refreshPage:page]; 
	
	if ([MCUserDefaults instance].dragListLastAndMore) {
		_reloading_up = NO;
		[refreshHeaderView_Up setState:EGOOPullRefreshNormal];
		[refreshHeaderView_Up refreshPage:page];
	}
}


- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
	[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
	int withFormButtonsBarHeight = [MCUIToolkit getWithFormButtonsBarHeight:withFormButtons buttonCol:buttonCol];
	if (withFormButtonsBarHeight != 0) {
		WithFormButtonsBar *withFormButtonsBar = [[WithFormButtonsBar alloc] initWithFrame:CGRectMake(0, 0, self.width, withFormButtonsBarHeight)];
		withFormButtonsBar.tag = -8888;
		[withFormButtonsBar addWithFormButtons:withFormButtons buttonCol:buttonCol delegate:self];
		self.tableview.tableFooterView = withFormButtonsBar;
		[withFormButtonsBar release];
	}
}
@end
